import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import './assets/style/resect.css'
import BaiduMap from 'vue-baidu-map'
import echarts from 'echarts'
import {
  Loading
} from 'element-ui'
import './assets/font_pjndg5tamdg/iconfont.css'
import Axios from 'axios'
import jquery from 'jquery';

Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8;'
import JsonExcel from 'vue-json-excel'
Vue.component('downloadExcel', JsonExcel)

Axios.defaults.withCredentials = true;
Vue.prototype.$ = jquery

Vue.config.productionTip = false
Vue.use(BaiduMap, {
  ak: 'VjNz8jH5uVjKkFqoXAvgjBApeijc0eKk'
})

Vue.prototype.$echarts = echarts;
Vue.prototype.$http = Axios;

// 路由配置
// const RouterConfig = {
//   mode: 'history',
//   routers: routers,
// }
// const router = new VueRouter(RouterConfig)
// const white = [LOGIN];
// const loginWhite = [INDEX];

/* 请求拦截器 */
Axios.interceptors.request.use(
  config => {
    // 判断是否存在token，如果存在的话，则每个http header都加上token
    let token = window.sessionStorage.getItem('token')
    if (!config.headers.hasOwnProperty('token') && token) {
      config.headers.token = token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  });

/* 响应拦截器 */
Axios.interceptors.response.use(function (response) { // ①10010 token过期（30天） ②10011 token无效
  if (response.data.resultcode === "Login-Reset-0000") {
    localStorage.removeItem('token') // 删除已经失效或过期的token（不删除也可以，因为登录后覆盖）
    router.replace({
      path: '/login' // 到登录页重新获取token
    })
  } else if (response.data.token) { // 判断token是否存在，如果存在说明需要更新token
    Storage.localSet('token', response.data.token) // 覆盖原来的token(默认一天刷新一次)
  }
  return response
}, function (error) {
  return Promise.reject(error)
})
Vue.prototype.openLoading = function() {
  const loading = Loading.service({
    lock: true,
    text: '正在加载...',
    spinner: 'el-icon-loading',
    background: '#F1F3F4',
    target: '.sub-main',
    body: true,
    customClass: 'mask'
  })
  setTimeout(function() {
    loading.close();
  },500)
  return loading;
}
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')